package com.intct.hbase.dml;

import com.intct.hbase.ddl.util.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-08-06 10:22
 */
public class PutCommand {
    public static void main(String[] args) throws IOException {
        // 创建HBase连接
//        Connection connection = ConnectionFactory.createConnection();
        Connection connection = HBaseConnection.getConnection();

        // TableName实例
        TableName tableName = TableName.valueOf("intct83", "t5");

        // 获取Admin实例
        Admin admin = connection.getAdmin();
        Table table = null;

        try {
            // 判断表存在
            if (admin.tableExists(tableName)) {
                // 获取Table实例
                table = connection.getTable(tableName);

                // 表名+Rowkey+列族+列限定符 == 存在  ==> 修改

                // Put对象 参数：rowkey
                Put put = new Put("1004".getBytes());
                // 插入数据
                //  family column
//                put.addColumn("f1".getBytes(), "msg".getBytes(), "msg info2".getBytes());
                put.addColumn("f1".getBytes(), "age".getBytes(), "18".getBytes());
//                put.addColumn("f1".getBytes(), "sex".getBytes(), "女".getBytes());

                table.put(put);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (table != null) {
                table.close();
            }
            if (admin != null) {
                admin.close();
            }
//            if (connection != null) {
//                connection.close();
//            }
        }
    }
}
